package CAD.shujuku;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Scanner;


/**
 * 这个类用来模拟用户登录
 * @author Administrator
 *
 */
public class LoginUser {

    private static Connection conn;
    private static ResultSet rs;
    private static PreparedStatement ps;
    private static int ad;

    //1.main方法，提示用户输入
    public static Scanner sc=new Scanner(System.in);
    public static void selection() throws SQLException {

        System.out.println("已显示全部职工信息");
        String sql="select * from staff";
        ps= JDBCUtils.getConnection().prepareStatement(sql);
        rs=ps.executeQuery();
        System.out.println("姓名"+"\t    "+"年龄"+"\t"+"    入职日期"+"\t"+"      工资"+"\t    "+"部门");
        while (rs.next()){

            String name = rs.getString("name");
            int age =rs.getInt("age");
            String hiredate = rs.getString("hiredate");
            int sal = rs.getInt("sal");
            String dept = rs.getString("dept");
            System.out.println(name+"\t    "+age+"\t  "+hiredate+"\t "+sal+"\t "+dept);


        }
    }
    public static  void  add() throws SQLException {
        System.out.println("请添加职工相关信息！！");
        System.out.print("姓名：");
        String name = sc.next();
        System.out.print("年龄：");
        int age =sc.nextInt();
        System.out.print("入职时间：");
        String hiredate = sc.next();
        System.out.print("工资：");
        int sal = sc.nextInt();
        System.out.print("部门：");
        String dept = sc.next();
        String sql="insert into staff() values('"+name+"','"+age+"','"+hiredate+"','"+sal+"','"+dept+"')";
        ps= JDBCUtils.getConnection().prepareStatement(sql);
        ad=ps.executeUpdate();
        System.out.println("添加成功！！！");
    }
    public static void update() throws SQLException {
        String sql;
        System.out.println("请输入你要修改职工信息的名字");
        String name = sc.next();
        sql= "select * from staff where name='"+name+"'";
        rs=ps.executeQuery();
        while (rs.next()){
            ad++;
        }
        if(ad>0){
            System.out.println("请选择修改的信息：1.名字2.年龄3.入职时间4.工资5.部门");
            int num = sc.nextInt();
            switch (num){
                case 1:
                    System.out.print("请输入新的名字：");
                    String newname = sc.next();
                    sql="update staff set name='"+newname+"'where name ='"+name+"'";
                    ps= JDBCUtils.getConnection().prepareStatement(sql);
                    ad=ps.executeUpdate();
                    break;
                case 2:
                    System.out.print("请输入新的年龄：");
                    int newage = sc.nextInt();
                    sql="update staff set name='"+newage+"'where name ='"+name+"'";
                    ps= JDBCUtils.getConnection().prepareStatement(sql);
                    ad=ps.executeUpdate();
                    break;
                case 3:
                    System.out.print("请输入新的入职时间：");
                    String newhiredate = sc.next();
                    sql="update staff set name='"+newhiredate+"'where name ='"+name+"'";
                    ps= JDBCUtils.getConnection().prepareStatement(sql);
                    ad=ps.executeUpdate();
                    break;
                case 4:
                    System.out.print("请输入新的工资：");
                    String newsal = sc.next();
                    sql="update staff set name='"+newsal+"'where name ='"+name+"'";
                    ps= JDBCUtils.getConnection().prepareStatement(sql);
                    ad=ps.executeUpdate();
                    break;
                case 5:
                    System.out.print("请输入新的部门：");
                    String newdept = sc.next();
                    sql="update staff set name='"+newdept+"'where name ='"+name+"'";
                    ps= JDBCUtils.getConnection().prepareStatement(sql);
                    ad=ps.executeUpdate();
                    break;
                default:
                    System.out.println("输入错误指示，将退出系统");
                    break;
            }
            if(ad==1){
                System.out.println("修改成功！！");
            }else{
                System.out.println("修改失败！！");
            }

        }else{
            System.out.println("输入名字不存在，将退出系统！！");
        }

    }
    public static void delete() throws SQLException {
        System.out.println("请你输入要删除的职工姓名");
        String name =sc.next();
        String sql= "delete from staff where name='"+name+"'";
        ps= JDBCUtils.getConnection().prepareStatement(sql);
        ad=ps.executeUpdate();
        if (ad==1){
            System.out.println("删除成功！！");
        }else{
            System.out.println("删除失败，不存在该员工，将退出系统！！");
        }


    }
    public  static void XMB() throws SQLException {
        System.out.println("是否进入职工管理系统（y:是/n:否）");
        String method = sc.next();
        int b=1;
        if("y".equals(method)||"Y".equals(method))
        {
            System.out.println("*********职工管理系统********");
            System.out.println("********1.查询职工信息********");
            System.out.println("********2.添加职工信息********");
            System.out.println("********3.修改职工信息********");
            System.out.println("********4.删除职工信息********");
            System.out.println("********5.退出系统...*********");
            System.out.println("请选择要操作的功能！！");
            int num = sc.nextInt();
            if(b==1){
            switch (num) {
                case 1:
                    LoginUser.selection();
                    break;
                case 2:
                    LoginUser.add();
                    break;
                case 3:
                    LoginUser.update();
                    break;
                case 4:
                    LoginUser.delete();
                    break;
                case 5:
                    System.out.println("已退出系统ing..");
                    break;
                default:
                    System.out.println("输入错误，请重新输入！！！");
                    break;
            }
            }
        }else if ("n".equals(method)||"N".equals(method)){
            System.out.println("已退出系统！！！");
        }

    }

    public static void main(String[] args) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("********职工管理系统********");
        System.out.println("**************************");
        System.out.print("请输入登录账号：");
        String user = sc.nextLine();
        System.out.print("请输入登录密码：");
        String password = sc.nextLine();
        //去user表根据用户输入的用户名和密码查询数据,利用jdbc技术
        login(user,password);
        if(login(user,password)){
            System.out.println("恭喜你！登录成功！");
            XMB();
        }else{
            System.out.println("您输入的用户名或密码错误,将退出系统！！");
        }



    }

    private static boolean login(String user, String password) {

        boolean a=true;

        try {
            //1.注册驱动
            //2.获取数据库连接
            conn = JDBCUtils.getConnection();
            //3.获取传输器
              String sql = "select * from stuff where user='"+user+"' and password='"+password+"' ";
           // String sql = "select * from stuff where user=? and password=?";
            //sql中包含？的叫做sql骨架，？叫做占位符；
            ps = conn.prepareStatement(sql);
            //给sql语句设置参数
//            ps.setString(1, user);
//            ps.setString(2, password);
            //4.执行sql
            rs = ps.executeQuery();
            //5.遍历结果集
            /*
             * 如果rs有值，就可以登录成功，反则登录失败
             */

            if(rs.next()){
                a=true;
            }else{
                a=false;
            }
            //6.释放资源`
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        finally {
//            JDBCUtils.close(rs, ps, conn);
//        }
        return a;
    }
}